<?php

namespace App\Api\Three;

/**
 * https://docs.qq.com/doc/DRUlEamNJWFFhVmVR
 */

use App\Api\ChannelApi;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Support\Facades\DB;


class MobileGD extends ChannelApi
{
    protected $model;
    protected $appKey = '';
    protected $url = '';
    protected $secret = '';
    protected $channel = '';
    protected $config = [];
    protected $HTTPOK = 1; //检查接口返回的数据 返回1 代表返回正确
    protected $root;
    protected $log;
    protected $isTrue = true;
    protected $return_message = '';
    protected $onLineNum = '';


    public function __construct($url = '', $str = '')
    {
        $this->pname = 'MobileGD';
        $this->log = new Logger('MobileGD');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/MobileGD.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->url = $url;
        // $this->url = 'https://221.179.11.204:443/eaop/rest/OrderCenter/resource';
        $this->url = 'https://221.179.11.204:443';
    }
    // 查询号码
    public function selectNums($array = [])
    {
        $mehtod = '/eaop/rest/BSS/commodity/querychoosenumberlist/v1.1.1';
        $data = [
            'sid' => '',
            'region' => "200",
            'tag' => "0",
            'page' => '1',
            'packagecode' => !empty($array['channel_code']) ? trim($array['channel_code'], ' ') : '789177253103472640',
            'recoid' => '789088566873464832',
        ];
        $res = $this->send($this->url . $mehtod, $data);
        // $res = '{"result":{"infos":[{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"15920341931"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"15113878340"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"18312422730"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"18719381424"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"18319440637"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"17820521276"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"17825821079"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"19876746914"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"19830943732"},{"region":"200","sid":"789177253103472640","commid":"789177253103472640","commtype":"50","tag":"0","cityname":"广州","mobileno":"19830430695"}]},"respcode":0,"respdesc":"success","resptype":"0"}';
        if (empty($res)) {
            return $this->returnMsg(200, '未查询到号码');
        }
        $returnData = json_decode($res, true);
        if (empty($returnData['result']['infos'])) {
            return $this->returnMsg(200, '已无号码');
        }
        $return = [];
        foreach ($returnData['result']['infos'] as $key => $v) {
            $return[] = [
                'city' => $v['cityname'],
                'iccid' => $v['mobileno'],
            ];
        }
        // var_dump($res);
        return $this->returnMsg(200, '查询成功', $return);;
    }
    // 地址效验  判断地址是否能发货
    public function addressVaild($array = [])
    {
        $mehtod = 'https://221.179.11.204:443/eaop/rest/JD/service/jdcheckaddress/v1.1.1';
        $array = [
            'operationrequest' => [
                'msgbody' => [
                    'provinceCode' => '',
                    'eparchyCode' => '',
                    'cityCode' => '',
                    'address' => '',
                ]
            ],
        ];
    }
    // 提交订单
    public function orderSubmit($array = [])
    {

        $method = '/eaop/rest/OrderCenter/resource/AirpickinstallnewOrder/v1.1.1';
        $data = [
            'operationrequest' => [
                'msgbody' => [],
            ],
        ];
        // '"wayid": "渠道编码",
        // "operatorId": "工号",
        // "areaCode": "200",
        // "areaName": "广州市",
        // "servnumber": "新购号码",
        // "userName": "姓名",
        // "acceptType": "2",
        // "receiveType": "4",
        // "payWay": "1",
        // "telno": "联系电话",
        // "cerNo": "身份证号",
        // "cerType": "01",
        // "province": "配送省份",
        // "addressCity": "配送地市",
        // "addressArea": "配送区域",
        // "address": "配送详细地址",
        // "mainprodid": "商品编码",
        // "orderId": "订单ID自己生成",
        // "offlineCard": "3",
        // "mainprodname": "商品名称",
        // "charge": "0",
        // "offerCompId": "",
        // "offerId": "商品编码",
        // "recoid": "789088566873464832"';
        $data = [
            'newOrderParams' => [
                'wayid' => 'GZ10EC221035',
                'operatorId' => 'GZ10ECC47848',
                'areaCode' => '200',
                'areaName' => '广州',
                'servnumber' => !empty($array['iccid']) ? $array['iccid'] : '15920341931',
                'userName' => $array['pay_name'],
                'acceptType' => "2",
                'receiveType' => "4",
                'payWay' => "1",
                'telno' => $array['pay_phone'],
                'cerNo' => $array['pay_identity'],
                'cerType' => '01',
                'province' => $array['post_province'],
                'addressCity' => $array['post_city'],
                'address' => $array['post_area'] . $array['post_address'],
                'mainprodid' => trim($array['channel_code'], ' '),
                'orderId' => $array['orderno'],
                'mainprodname' => '89元畅享卡升级版',
                'offlineCard' => "3",
                "charge" => "0",
                "offerCompId" => "",
                "offerId" =>  trim($array['channel_code'], ' '),
                "recoid" => "789088566873464832"
            ]
        ];
        $res = $this->send($this->url . $method, $data);
        // 组装提交数据
        // $method = '/AirpickinstallnewOrder/v1.1.1';
        // $this->log->info('参数:' . json_encode($array));
        // $data = $this->setData($array);
        // if (empty($data)) {
        //     return $this->returnMsg(100, '传递参数为空');
        // }
        // $strArr = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        // $headers = [
        //     'Content-Type:application/json;charset=utf-8',
        //     'X-Sign:' . $this->getSign($strArr),
        // ];
        // $url = $this->url . $method;
        // $res =  $this->sendBefare($url, $strArr, $headers);
        // $this->log->info('参数:' . ($strArr) . '返回的参数:' . $res);
        if (!empty($res)) {
            $res = json_decode($res, true);
        }
        if (!empty($res)) {
            return $this->returnMsg(200, '提交成功', ['order_id' => !empty($res['result']['orderid']) ? $res['result']['orderid'] : '']);
        }
        return $this->returnMsg(100, '提交失败', ['order_id' => !empty($res['respdesc']) ? $res['respdesc'] : '']);
    }
    // 不同套餐 设置的参数不一样
    public function setData($data)
    {
        $return = [];
        switch ($data['channel_code']) {
            case '29元套餐':
                $return = [
                    'order_data' => [
                        "代理商账号" => "yd-hnyx",
                        "使用人姓名" => "王园园",
                        "使用人证件号" => "412728199107115726",
                        "接口下单账号" => 'agentInterface',
                        '异步通知地址' => '',
                        "业务类型" => "赋能产品业务",
                        "办理类型" => "新装主卡",
                        "发票信息" => [
                            '发票投递方式' => '不需要',
                        ],
                        "客户信息" => [
                            "客户类型" => "普通客户",
                            "客户证件号码" => $data['pay_identity'],
                            "客户证件姓名" => $data['pay_name'],
                            "客户证件类型" => "身份证",
                            "联系人" => $data['pay_name'],
                            "联系电话" => $data['pay_phone']
                        ],
                        "揽装信息" => [
                            "协销工号" => "",
                            "揽装工号" => "30810881"
                        ],
                        "物流信息" => [
                            "发货类型" => "同德仓发货",
                            "受理类型" => "不需要受理",
                            "回收资料" => "",
                            "配送公司" => "EMS",
                            "配送失败是否需要回退" => "否",
                            "配送类型" => "普通服务"
                        ],
                        "移动接入" => [
                            [
                                "UIM实物串号" => "",
                                "号码类型" => "主卡",
                                "移动接入号" => "随机",
                                "订购类型" => "新装"
                            ]
                        ],
                        "订单备注" => "",
                        "订单来源单号" => $data['orderno'],
                        "订购产品" => [
                            "AI编码" => "B2XPT220220509141550882812",
                            "产品名称" => "【广州】39元星卡2022版-预存100通行证抵套内",
                            "付费类型" => "预付费",
                            '属性列表' => [
                                [
                                    '属性值' => "18037155139",
                                    '属性名' => "销售人员账号"
                                ]
                            ]
                        ],
                        '配送信息' => [
                            '区' => $data['post_area'],
                            '市' => $data['post_city'],
                            '省' => $data['post_province'],
                            '联系人' => $data['pay_name'],
                            '联系电话' => $data['pay_phone'],
                            '详细地址' => $data['post_address'],
                            '配送产品' => [
                                [
                                    "产品名称" => "分销B2I卡",
                                    "产品数量" => "1",
                                    "产品类型" => "号卡"
                                ],
                                [
                                    "产品名称" => "东山大王星卡卡套DSFX2901",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ],
                                [
                                    "产品名称" => "举牌照片说明卡套GZS",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ]
                            ],
                        ],

                    ],
                ];
                break;
            case '49元套餐':
                $return = [
                    'order_data' => [
                        "代理商账号" => "yd-hnyx",
                        "使用人姓名" => "王园园",
                        "使用人证件号" => "412728199107115726",
                        "接口下单账号" => 'agentInterface',
                        '异步通知地址' => '',
                        "业务类型" => "赋能产品业务",
                        "办理类型" => "新装主卡",
                        "发票信息" => [
                            '发票投递方式' => '不需要',
                        ],
                        "客户信息" => [
                            "客户类型" => "普通客户",
                            "客户证件号码" => $data['pay_identity'],
                            "客户证件姓名" => $data['pay_name'],
                            "客户证件类型" => "身份证",
                            "联系人" => $data['pay_name'],
                            "联系电话" => $data['pay_phone']
                        ],
                        "揽装信息" => [
                            "协销工号" => "",
                            "揽装工号" => "30810881"
                        ],
                        "物流信息" => [
                            "发货类型" => "同德仓发货",
                            "受理类型" => "不需要受理",
                            "回收资料" => "",
                            "配送公司" => "EMS",
                            "配送失败是否需要回退" => "否",
                            "配送类型" => "普通服务"
                        ],
                        "移动接入" => [
                            [
                                "UIM实物串号" => "",
                                "号码类型" => "主卡",
                                "移动接入号" => "随机",
                                "订购类型" => "新装"
                            ]
                        ],
                        "订单备注" => "",
                        "订单来源单号" => $data['orderno'],
                        "订购产品" => [
                            "AI编码" => " B2XPT220220519154613346827",
                            "产品名称" => "【广州】39元星卡2022版返10-预存100通行证抵套内-30元流量包",
                            "付费类型" => "预付费",
                            '属性列表' => [
                                [
                                    '属性值' => "18037155139",
                                    '属性名' => "销售人员账号"
                                ]
                            ]
                        ],
                        '配送信息' => [
                            '区' => $data['post_area'],
                            '市' => $data['post_city'],
                            '省' => $data['post_province'],
                            '联系人' => $data['pay_name'],
                            '联系电话' => $data['pay_phone'],
                            '详细地址' => $data['post_address'],
                            '配送产品' => [
                                [
                                    "产品名称" => "分销B2I卡",
                                    "产品数量" => "1",
                                    "产品类型" => "号卡"
                                ],
                                [
                                    "产品名称" => "东山大王星卡卡套DSFX4901",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ],
                                [
                                    "产品名称" => "举牌照片说明卡套GZS",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ]
                            ],
                        ],

                    ],
                ];
                break;
            case '40元套餐2':
                $return = [
                    'order_data' => [
                        "代理商账号" => "yd-hnyx",
                        "使用人姓名" => "王园园",
                        "使用人证件号" => "412728199107115726",
                        "接口下单账号" => 'agentInterface',
                        '接口下单分发账号' => '',
                        '接口下单分发账号名称' => '',
                        '异步通知地址' => '',
                        "业务类型" => "移动业务",
                        "办理类型" => "新装主卡",
                        "发票信息" => [
                            '发票投递方式' => '不需要',
                        ],
                        "客户信息" => [
                            "客户类型" => "普通客户",
                            "客户证件号码" => $data['pay_identity'],
                            "客户证件姓名" => $data['pay_name'],
                            "客户证件类型" => "身份证",
                            "联系人" => $data['pay_name'],
                            "联系电话" => $data['pay_phone']
                        ],
                        "揽装信息" => [
                            "协销工号" => "",
                            "揽装工号" => "30810881"
                        ],
                        '一次性费用项' => [
                            '费用项列表' => [
                                [
                                    '缴费方式' => '穗易付',
                                    '费用项名称' => '预存100元得220元（抵扣套餐费预存接续）',
                                    '费用项类型' => '',
                                    '费用项金额' => '100',
                                ]
                            ],
                        ],
                        '月租费用项' => [
                            '缴费方式' => '现金支付',
                            '费用项列表' => [
                                [
                                    '费用项名称' => '39元星卡-星卡1元升级A计划'
                                ]
                            ],
                        ],
                        "物流信息" => [
                            "发货类型" => "同德仓发货",
                            "受理类型" => "后置受理",
                            "回收资料" => "",
                            "配送公司" => "EMS",
                            "配送失败是否需要回退" => "否",
                            "配送类型" => "高值服务"
                        ],
                        "移动接入" => [
                            [
                                "UIM实物串号" => "",
                                "号码类型" => "主卡",
                                "移动接入号" => "随机",
                                "订购类型" => "新装"
                            ]
                        ],
                        "订单备注" => "",
                        "订单来源单号" => $data['orderno'],
                        "订购产品" => [
                            "产品名称" => "39元星卡-星卡1元升级A计划",
                            "付费类型" => "预付费",
                            '属性列表' => [
                                [
                                    '属性值' => "权益大礼包",
                                    '属性名' => "首月优惠（40元）"
                                ],
                                [
                                    '属性值' => "应用权益",
                                    '属性名' => "套餐升级20GB流量（促销）"
                                ]
                            ]
                        ],
                        '配送信息' => [
                            '区' => $data['post_area'],
                            '市' => $data['post_city'],
                            '省' => $data['post_province'],
                            '联系人' => $data['pay_name'],
                            '联系电话' => $data['pay_phone'],
                            '详细地址' => $data['post_address'],
                            '配送产品' => [
                                [
                                    "产品名称" => "分销B2I卡",
                                    "产品数量" => "1",
                                    "产品类型" => "号卡"
                                ],
                                [
                                    "产品名称" => "本地星卡A计划卡套FXZ120",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ]
                            ],
                        ],

                    ],
                ];
                break;
            case '30元套餐':
                $return = [
                    'order_data' => [
                        "代理商账号" => "yd-hnyx",
                        "使用人姓名" => "王园园",
                        "使用人证件号" => "412728199107115726",
                        "接口下单账号" => 'agentInterface',
                        '接口下单分发账号' => '',
                        '接口下单分发账号名称' => '',
                        '异步通知地址' => '',
                        "业务类型" => "赋能产品业务",
                        "办理类型" => "新装主卡",
                        "发票信息" => [
                            '发票投递方式' => '不需要',
                        ],
                        "客户信息" => [
                            "客户类型" => "普通客户",
                            "客户证件号码" => $data['pay_identity'],
                            "客户证件姓名" => $data['pay_name'],
                            "客户证件类型" => "身份证",
                            "联系人" => $data['pay_name'],
                            "联系电话" => $data['pay_phone']
                        ],
                        "揽装信息" => [
                            "协销工号" => "",
                            "揽装工号" => "30810881"
                        ],
                        "物流信息" => [
                            "发货类型" => "同德仓发货",
                            "受理类型" => "不需要受理",
                            "回收资料" => "",
                            "配送公司" => "EMS",
                            "配送失败是否需要回退" => "否",
                            "配送类型" => "普通服务"
                        ],
                        "移动接入" => [
                            [
                                "UIM实物串号" => "",
                                "号码类型" => "主卡",
                                "移动接入号" => "随机",
                                "订购类型" => "新装"
                            ]
                        ],
                        "订单备注" => "",
                        "订单来源单号" => $data['orderno'],
                        "订购产品" => [
                            "AI编码" => 'B2XPT220220530091813184840',
                            "产品名称" => "【广州】39元星卡2022版-计划A",
                            "付费类型" => "预付费",
                            '属性列表' => [
                                [
                                    '属性值' => "18037155139",
                                    '属性名' => "销售人员账号"
                                ]
                            ]
                        ],
                        '配送信息' => [
                            '区' => $data['post_area'],
                            '市' => $data['post_city'],
                            '省' => $data['post_province'],
                            '联系人' => $data['pay_name'],
                            '联系电话' => $data['pay_phone'],
                            '详细地址' => $data['post_address'],
                            '配送产品' => [
                                [
                                    "产品名称" => "分销B2I卡",
                                    "产品数量" => "1",
                                    "产品类型" => "号卡"
                                ],
                                [
                                    "产品名称" => "预存实名指引A计划卡套FXZ00A",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ],
                                [
                                    "产品名称" => "举牌照片说明卡套GZS",
                                    "产品数量" => "1",
                                    "产品类型" => "礼品"
                                ]
                            ],
                        ],

                    ],
                ];
                break;
        }
        return $return;
    }
    // 查询订单 查询
    public function queryOrderStatus($array = [])
    {
        $method = '/eaop/rest/OrderCenter/resource/dsairpickinstallqueryorder/v1.1.1';
        $data = [
            'orderId' => $array['sup_orderno'],
        ];
        $res = $this->send($this->url . $method, $data);
        // var_dump($res);
        if (!empty($res)) {
            $res = json_decode($res, true);
        }
        return $this->returnMsg(200, '查询成功', $res);
    }
    // 处理数据 将数据处理成统一格式
    public function queryOneOrder($array = [])
    {
        $res = $this->queryOrderStatus($array);
        if (empty($res['data'])) {
            return $this->returnMsg(100, '数据为空');
        }
        if (empty($res['data']['result']['order'])) {
            return $this->returnMsg(101, '未查询到数据');
        }
        $getData = $res['data']['result']['order'][0];
        $statusDatas = [
            0 => '提交中',
            1 => '提交上游',
            // 2 => '拒收单',
            10 => '审核中',
            11 => '待发货',
            20 => '已发货',
            30 => '已签收',
            40 => '已激活',
            50 => '异常单',
            51 => '取消单',
            60 => '失败单'
        ];
        $statusArr = [
            '450' => '待开卡',
            '550' => '已发货',
            '460' => '已领取',
            '500' => '待发货',
            '-460' => '开卡失败',
            '-200' => '身份审核失败',
            '-100' => '待支付',
            '200' => '待审核',
            '120' => '初步审核'
        ];;
        $returnStatusArr = array(
            '550' => 20,
            '-460' => 60,
            '-200' => 50,
            '待办理' => 1,
            '460' => 30,
            '500' => 11,
            '200' => 10,
            '470' => 10,
            '-100' => 1,
            '120' => 10,
            '0' => 51
        );
        $data = [
            'id' => $getData['orderId'],
            'sup_order' => $getData['orderId'],
            'created_at' => date('Y-m-d H:i:s', strtotime($getData['createTime'])),
            'order_desc' => $getData['orderRemark'],
            'order_num' => $getData['orderId'],
            // 'pagename' => $getData['订购产品']['产品名称'],
            'express_company' => '京东配送',
            'express_number' => !empty($getData['expressno']) ? $getData['expressno'] : '',
            'status' => !empty($returnStatusArr[$getData['orderStatus']]) ? $returnStatusArr[$getData['orderStatus']] : '',
        ];
        return $this->returnMsg(200, '查询成功', [$data]);
    }
    // 统一发送信息
    public function send($url, $data)
    {
        $times = date('YmdHis') . '000';
        $round1 = mt_rand(000000000, 999999999);
        $round = mt_rand(000000000, 999999999);
        $headers = [
            'appId:104600',
            'timestamp:' . $times,
            'busiSerial:' . md5($times . $round),
            'nonce:' . strtoupper(md5($times . $round1)),

        ];
        // 加密字符串组装
        $signs = 'appId=' . 104600 . '&busiSerial=' . md5($times . $round) . '&nonce=' . strtoupper(md5($times . $round1)) . '&timestamp=' . $times . json_encode($data);
        $headers[] = 'sign:' . $this->getSign($signs);
        $res =  $this->sendBefare($url, $data, $headers);
        $this->log->info('header头参数' . json_encode($headers) . '参数' . json_encode($data) . '返回结果' . json_encode($res));
        return $res;
    }
    public function sendBefare($url, $data, $headers, $isPost = 1)
    {
        // return 1212222222222;
        // var_dump($isPost);
        $curl = curl_init(); // 启动一个CURL会话
        if (!empty($isPost) && $isPost != 1) {
            $url = $url . '?data=' . urlencode($data);
        }
        $headers[] = 'Content-Type:application/json;charset=utf-8';
        // var_dump($url, 666666666);
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        if ($isPost == 1) {
            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); // Post提交的数据包
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $result = curl_exec($curl); // 执行操作
        // $error = curl_errno($curl);
        if (curl_error($curl)) {

            // var_dump(curl_error($curl), 3333333333);
        }
        // var_dump($error);
        curl_close($curl); // 关闭CURL会话
        return $result;
    }
    public function sendBefareGet($url, $data, $headers)
    {
        $curl = curl_init(); // 启动一个CURL会话
        $url = $url . '?data=' . urlencode($data);
        // var_dump($url, 666666666);
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        $result = curl_exec($curl); // 执行操作
        // var_dump($result, 111111111);
        curl_close($curl); // 关闭CURL会话
        return $result;
    }
    public function cs()
    {
        $str = 'appId=501143&busiSerial=0.31823.173.17060211000635212345&nonce=1234567890&timestamp=20170802094500123{"infoid": "2","servnumber": "1589187976","authid": "bairong_20170616_15989187976_0912398123"}';
        $res = $this->getSign($str);
        return $res;
    }
    public function getSign($str)
    {
        if (is_array($str)) {
            $str = json_encode($str, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }
        // $strs = strtolower(md5($str));
        $getPKey = $this->getPrivate();
        $key = openssl_pkey_get_private($getPKey);
        // $return = openssl_private_encrypt($str, $crypted, $key);
        $return = openssl_sign($str, $crypted, $key, OPENSSL_ALGO_SHA256);
        if (!$return) {
            return '';
        }
        return base64_encode($crypted);
    }
    public function getPrivate()
    {
        $str = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEXCv/ysvWnTrZ79x/neB3ZBgiPndR2gD5YWLiywRdQpp0i0aqAxJ97nk7XHBIXrRYpEZbEuSWIT4AmxD86x9k9WcCj3dMDC68SpXTEY67NaPjpHLGHwACwBfQeagfwrEhl4CUW9Jzs9rR87iuWA6H+qTJnBmnQho+9FBEPyDfcEA3ePLhOEEw8QP8eQewKxM7KEiUru7K9nFs1A6dmao02rEWm/EAxlCBT6PnhUtiswD55xt8dnWKDl5B31DDO3PaV8UjdtJoIsWpzaEmu8fSRr/BrQ1dpIxf0OfzJTV4nc6bW7S+5cNKLso4cdNOBMM9DAqEse7Eef1kjCPDtUu/AgMBAAECgg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iQKBgQDahlOJovvGJoTE7dQFi/vO9apvznCqoHskctLP7lcs/sI+NsqyMIxEzK9VIWQ2HJQ6onC/Khf2dw18ALgalIdi5W4iOn9UFhigcrHMz/LImCq/kv1W/dGDeJOchIvCN2jxlhOTIpQvS7pPbCISeqHW5VRXtKKpECvKJ3X95k26owKBgQDCFzKHhViyPrK8mlE1M0gLxUVskQIqBU0FpopyyU8D2K/GLFFT3ybxsuF62yKJ7XRDQHoZadL47COGUGYWNLsXzZGHJ88D6kgl4qUwAXG1A0us6QNeANnfJjwevLHqD1OEn9AKvX2/kFotmdsDftD0PNWX5WoqwOLyrLhF3vxVaQKBgQCkfpshr2thNJua9gA1ELPDjc/3i+nptxYbUMf6N8qtlzQ2yt34Fm1wWLffGFysEwVNwfunGxOgtuuSnaP4djgXrOcjou84J83v8YZ/y0VR1KHRRd+ofTYahkhveKMP1tALk1I23r+3foTHxnk7ec6bylOcFeCf7QlZiizarGN0iwKBgQCxSEsVPuTqXDRhK/FUULi0IZqsO0LhpEyXhUX03XkEHHf+H0fsz1/s0oUeQzWpPtcSHYodO7TuE0zlkusx7UNDuzsXajEbr87QXhtQ8KdZHI4hg6R/fE4M4RG4QxwjgzUyi1FkzryUEhXof9+TAVRAay/qAf7uyei05medIXMatQ==";
        $strs = "-----BEGIN PRIVATE KEY-----\n" . chunk_split($str, 64, "\n") . "-----END PRIVATE KEY-----";
        // var_dump($strs);
        return $strs;
    }
    public function getSign_back($str)
    {
        if (is_array($str)) {
            $str = json_encode($str, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }
        $strs = strtolower(md5($str));
        $getPKey = $this->getPublicKey();
        $key = openssl_pkey_get_public($getPKey);
        $return = openssl_public_encrypt($strs, $crypted, $key);
        if (!$return) {
            return '';
        }
        return base64_encode($crypted);
    }
    public function getPublicKey()
    {
        $str = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAok8DnvM2OBwtzNoJb4nA
jn4lgCkSxLyRw5lLiUfsi+40h00CGAC1arvbinTTbtrSnm4YDYlaJwveiOeDKdk0
f2Yglb/8loEbNTqb1OqNxUeM1wYLRBXvAkkZ5fZllajWzFMkoNU/JQ4+T8sMATxm
1d1XRcgM6zlgVwX6siDoLZ5Dm23CTS7SXhyMLtS9cgLefHcOckPQyXoB/IJqb0K1
P0Sk0PE1OSRzpsI9JVzhsSW164fwYpxEbygj9O9JAmAszixwItt2j5zLJ6zRnK3R
OrcAVMN/vpXTN2dCLGZXQrsTK9sAWASzIWTiQRzmnwxsQY7y4u2h+k41n8Z5tw03
wwIDAQAB
-----END PUBLIC KEY-----";
        // $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAok8DnvM2OBwtzNoJb4nAjn4lgCkSxLyRw5lLiUfs+40h00CGAC1arvbinTTbtrSnm4YDYlaJwveiOeDKdk0f2Yglb/8loEbNTqb1OqNxUeM1wYLRBXvAkkZ5fZllajWzFMkoNU/JQ4+T8sMATxm1d1XRcgM6zlgVwX6siDoLZ5Dm23CTS7SXhyMLtS9cgLefHcOckPQyXoB/IJqb0K1P0Sk0PE1OSRzpsI9JVzhsSW164fwYpxEbygj9O9JAmAszixwItt2j5zLJ6zRnK3ROrcAVMN/vpXTN2dCLGZXQrsTK9sAWASzIWTiQRzmnwxsQY7y4u2h+k41n8Z5tw03wwIDAQAB';
        // $str = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
        return $str;
    }
}
